﻿using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.OrcanCommand;
using B.S.Domain.Accounts;
using B.S.Domain.Dto;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.Management.SteamApplication.CommandHandler.OrcanCommandHandler
{
    public class OrcanQueryCommandHandler : IRequestHandler<OrcanQueryCommand, APIRequst<APIPageing<List<OrcantTreesDTO>>>>
    {
        public OrcanQueryCommandHandler(IBaseRepository<OrcanClass> orcanClassRepository,IBaseRepository<DeptClass> deptClassRepository)
        {
            OrcanClassRepository = orcanClassRepository;
            DeptClassRepository = deptClassRepository;
        }

        public IBaseRepository<OrcanClass> OrcanClassRepository { get; }
        public IBaseRepository<DeptClass> DeptClassRepository { get; }

        public async Task<APIRequst<APIPageing<List<OrcantTreesDTO>>>> Handle(OrcanQueryCommand request, CancellationToken cancellationToken)
        {
            var path = OrcanClassRepository.GetAll();
            if(!string.IsNullOrEmpty(request.OrcanName))
            {
                path = path.Where(x => x.OranCord.Contains(request.OrcanCord));
            }
            var pats = DeptClassRepository.GetAll();
            if(!string.IsNullOrEmpty(request.OrcanName))
            {
                pats = pats.Where(x => x.DeptName.Contains(request.OrcanName));
            }

            var listDto=await GetOrcanTree(0);
            var totalCount = listDto.Count();
            var pageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            var list = listDto.OrderBy(a => a.OranId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return new APIRequst<APIPageing<List<OrcantTreesDTO>>>
            {
                Code = RequstCode.成功,
                Msg = "",
                Data = new APIPageing<List<OrcantTreesDTO>>
                {
                    Total = pageCount,
                    TotalCount = totalCount,
                    Data = list
                }
            };
        }

        public  async Task<List<OrcantTreesDTO>> GetOrcanTree(int id)
        {
            var listdto = from a in OrcanClassRepository.GetAll()
                          join b in DeptClassRepository.GetAll() on a.DeptId equals b.DeptId
                          where a.OrganProId == id
                          select new OrcantTreesDTO
                          {
                              OranId = a.OranId,
                              OranCord = a.OranCord,
                              DeptId = a.DeptId,
                              OrganProId = a.OrganProId,
                              DeptName = b.DeptName,
                              OranCordName=a.OranCordName

                          };
           var DTO=await listdto.ToListAsync();
            List<OrcantTreesDTO> trees = new List<OrcantTreesDTO>();
            foreach (var item in DTO)
            {
                OrcantTreesDTO tree = new OrcantTreesDTO
                {
                    OranId = item.OranId,
                     OranCord= item.OranCord,
                      DeptId = item.DeptId,
                       DeptName = item.DeptName,
                        OrganProId = item.OrganProId,
                    OranCordName=item.OranCordName,
                        children =await GetOrcanTree(item.OranId)

                };
                trees.Add(tree);
            }
            return trees;
        }
        
    }
}
